﻿//
// Created by francklinson on 2021/3/18.
//

#include <iostream>

template<typename T>
// template A
void ShowArray(T arr[], int n); // 元素为数组

template<typename T>
// template B
void ShowArray(T *arr[], int n); // 元素为指针数组


struct debts
{
    char name[50];
    double amount;
};

int main()
{
    using namespace std;
    int things[6] = {13, 31, 103, 301, 310, 130};
    debts mr_E[3] = // 结构体数组
            {
                    {"Ima Wolfe", 2400.0},
                    {"Ura Foxe",  1300.0},
                    {"Iby Stout", 1800.0}
            };
    double *pd[3];

// set pointers to the amount members of the structures in mr_E
    for (int i = 0; i < 3; i++)
        pd[i] = &mr_E[i].amount;// 指针数组，每个指针指向结构体数组中每个结构体的amount数据

    cout << "Listing Mr. E's counts of things:\n";
// things is an array of int
    ShowArray(things, 6); // uses template A
    cout << "Listing Mr. E's debts:\n";
// pd is an array of pointers to double
    ShowArray(pd, 3); // uses template B (more specialized)
    return 0;
}

template<typename T>
void ShowArray(T arr[], int n)
{
    using namespace std;
    cout << "template A\n";
    for (int i = 0; i < n; i++)
        cout << arr[i] << ' ';
    cout << endl;
}

template<typename T>
void ShowArray(T *arr[], int n)
{
    using namespace std;
    cout << "template B\n";
    for (int i = 0; i < n; i++)
        cout << *arr[i] << ' '; // 指针数组每个元素的解引用是结构体数组中每个结构体的amount属性
    cout << endl;
}

// 如果将第二个函数模板的重载版本删除，将会调用第一个函数模板，显示amount属性的地址